#ifndef THINK_THINK_MODEL_RELATION_MORPHONE_H
#define THINK_THINK_MODEL_RELATION_MORPHONE_H

static char* think_model_relation_morphone =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\model\\relation;\n"
	"\n"
	"use think\\db\\Query;\n"
	"use think\\Exception;\n"
	"use think\\Loader;\n"
	"use think\\Model;\n"
	"use think\\model\\Relation;\n"
	"\n"
	"class MorphOne extends Relation\n"
	"{\n"
	"    // 多态字段\n"
	"    protected $morphKey;\n"
	"    protected $morphType;\n"
	"    // 多态类型\n"
	"    protected $type;\n"
	"\n"
	"    /**\n"
	"     * 构造函数\n"
	"     * @access public\n"
	"     * @param Model  $parent    上级模型对象\n"
	"     * @param string $model     模型名\n"
	"     * @param string $morphKey  关联外键\n"
	"     * @param string $morphType 多态字段名\n"
	"     * @param string $type      多态类型\n"
	"     */\n"
	"    public function __construct(Model $parent, $model, $morphKey, $morphType, $type)\n"
	"    {\n"
	"        $this->parent    = $parent;\n"
	"        $this->model     = $model;\n"
	"        $this->type      = $type;\n"
	"        $this->morphKey  = $morphKey;\n"
	"        $this->morphType = $morphType;\n"
	"        $this->query     = (new $model)->db();\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 延迟获取关联数据\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包查询条件\n"
	"     * @return false|\\PDOStatement|string|\\think\\Collection\n"
	"     */\n"
	"    public function getRelation($subRelation = \'\', $closure = null)\n"
	"    {\n"
	"        if ($closure) {\n"
	"            call_user_func_array($closure, [ & $this->query]);\n"
	"        }\n"
	"        $relationModel = $this->relation($subRelation)->find();\n"
	"\n"
	"        if ($relationModel) {\n"
	"            $relationModel->setParent(clone $this->parent);\n"
	"        }\n"
	"\n"
	"        return $relationModel;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 根据关联条件查询当前模型\n"
	"     * @access public\n"
	"     * @param string  $operator 比较操作符\n"
	"     * @param integer $count    个数\n"
	"     * @param string  $id       关联表的统计字段\n"
	"     * @param string  $joinType JOIN类型\n"
	"     * @return Query\n"
	"     */\n"
	"    public function has($operator = \'>=\', $count = 1, $id = \'*\', $joinType = \'INNER\')\n"
	"    {\n"
	"        return $this->parent;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 根据关联条件查询当前模型\n"
	"     * @access public\n"
	"     * @param  mixed $where  查询条件（数组或者闭包）\n"
	"     * @param  mixed $fields 字段\n"
	"     * @return Query\n"
	"     */\n"
	"    public function hasWhere($where = [], $fields = null)\n"
	"    {\n"
	"        throw new Exception(\'relation not support: hasWhere\');\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询\n"
	"     * @access public\n"
	"     * @param array    $resultSet   数据集\n"
	"     * @param string   $relation    当前关联名\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包\n"
	"     * @return void\n"
	"     */\n"
	"    public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)\n"
	"    {\n"
	"        $morphType = $this->morphType;\n"
	"        $morphKey  = $this->morphKey;\n"
	"        $type      = $this->type;\n"
	"        $range     = [];\n"
	"        foreach ($resultSet as $result) {\n"
	"            $pk = $result->getPk();\n"
	"            // 获取关联外键列表\n"
	"            if (isset($result->$pk)) {\n"
	"                $range[] = $result->$pk;\n"
	"            }\n"
	"        }\n"
	"\n"
	"        if (!empty($range)) {\n"
	"            $data = $this->eagerlyMorphToOne([\n"
	"                $morphKey  => [\'in\', $range],\n"
	"                $morphType => $type,\n"
	"            ], $relation, $subRelation, $closure);\n"
	"            // 关联属性名\n"
	"            $attr = Loader::parseName($relation);\n"
	"            // 关联数据封装\n"
	"            foreach ($resultSet as $result) {\n"
	"                if (!isset($data[$result->$pk])) {\n"
	"                    $relationModel = null;\n"
	"                } else {\n"
	"                    $relationModel = $data[$result->$pk];\n"
	"                    $relationModel->setParent(clone $result);\n"
	"                    $relationModel->isUpdate(true);\n"
	"                }\n"
	"\n"
	"                $result->setRelation($attr, $relationModel);\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询\n"
	"     * @access public\n"
	"     * @param Model    $result      数据对象\n"
	"     * @param string   $relation    当前关联名\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包\n"
	"     * @return void\n"
	"     */\n"
	"    public function eagerlyResult(&$result, $relation, $subRelation, $closure)\n"
	"    {\n"
	"        $pk = $result->getPk();\n"
	"        if (isset($result->$pk)) {\n"
	"            $pk   = $result->$pk;\n"
	"            $data = $this->eagerlyMorphToOne([\n"
	"                $this->morphKey  => $pk,\n"
	"                $this->morphType => $this->type,\n"
	"            ], $relation, $subRelation, $closure);\n"
	"\n"
	"            if (isset($data[$pk])) {\n"
	"                $relationModel = $data[$pk];\n"
	"                $relationModel->setParent(clone $result);\n"
	"                $relationModel->isUpdate(true);\n"
	"            } else {\n"
	"                $relationModel = null;\n"
	"            }\n"
	"\n"
	"            $result->setRelation(Loader::parseName($relation), $relationModel);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 多态一对一 关联模型预查询\n"
	"     * @access   public\n"
	"     * @param array         $where       关联预查询条件\n"
	"     * @param string        $relation    关联名\n"
	"     * @param string        $subRelation 子关联\n"
	"     * @param bool|\\Closure $closure     闭包\n"
	"     * @return array\n"
	"     */\n"
	"    protected function eagerlyMorphToOne($where, $relation, $subRelation = \'\', $closure = false)\n"
	"    {\n"
	"        // 预载入关联查询 支持嵌套预载入\n"
	"        if ($closure) {\n"
	"            call_user_func_array($closure, [ & $this]);\n"
	"        }\n"
	"        $list     = $this->query->where($where)->with($subRelation)->find();\n"
	"        $morphKey = $this->morphKey;\n"
	"        // 组装模型数据\n"
	"        $data = [];\n"
	"        foreach ($list as $set) {\n"
	"            $data[$set->$morphKey][] = $set;\n"
	"        }\n"
	"        return $data;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 保存（新增）当前关联数据对象\n"
	"     * @access public\n"
	"     * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键\n"
	"     * @return Model|false\n"
	"     */\n"
	"    public function save($data)\n"
	"    {\n"
	"        if ($data instanceof Model) {\n"
	"            $data = $data->getData();\n"
	"        }\n"
	"\n"
	"        // 保存关联表数据\n"
	"        $pk = $this->parent->getPk();\n"
	"\n"
	"        $data[$this->morphKey]  = $this->parent->$pk;\n"
	"        $data[$this->morphType] = $this->type;\n"
	"\n"
	"        $model = new $this->model();\n"
	"\n"
	"        return $model->save() ? $model : false;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 创建关联对象实例\n"
	"     * @param array $data\n"
	"     * @return Model\n"
	"     */\n"
	"    public function make($data = [])\n"
	"    {\n"
	"        if ($data instanceof Model) {\n"
	"            $data = $data->getData();\n"
	"        }\n"
	"        // 保存关联表数据\n"
	"        $pk = $this->parent->getPk();\n"
	"\n"
	"        $data[$this->morphKey]  = $this->parent->$pk;\n"
	"        $data[$this->morphType] = $this->type;\n"
	"\n"
	"        return new $this->model($data);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 执行基础查询（进执行一次）\n"
	"     * @access protected\n"
	"     * @return void\n"
	"     */\n"
	"    protected function baseQuery()\n"
	"    {\n"
	"        if (empty($this->baseQuery) && $this->parent->getData()) {\n"
	"            $pk                    = $this->parent->getPk();\n"
	"            $map[$this->morphKey]  = $this->parent->$pk;\n"
	"            $map[$this->morphType] = $this->type;\n"
	"            $this->query->where($map);\n"
	"            $this->baseQuery = true;\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 创建关联统计子查询\n"
	"     * @access public\n"
	"     * @param \\Closure $closure 闭包\n"
	"     * @param string   $name    统计数据别名\n"
	"     * @return string\n"
	"     */\n"
	"    public function getRelationCountQuery($closure, &$name = null)\n"
	"    {\n"
	"        throw new Exception(\'relation not support: withCount\');\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif